package org.xxforest.game.core.assets;

import java.util.HashMap;
import java.util.Map;

import org.xxforest.game.core.common.XXDisposable;

public class XXAssetManager implements XXDisposable {

	private Map<String,XXAsnycLoader> loaders ;
	
	private XXAssetManager(){
		loaders = new HashMap<String,XXAsnycLoader>();
	}
	
	public static XXAssetManager create(){
		return new XXAssetManager() ;
	}
	
	/**
	 * 添加需要一步加载的资源
	 * @param loader
	 */
	public void addLoader(XXAsnycLoader loader){
		loader.loadAsync() ;
		loaders.put(loader.getFileName(),loader);
	}
	
	/**
	 * 添加异步加载的纹理
	 * @param fileName	纹理路径
	 */
	public void addTextureLoader(String fileName){
		addLoader(XXTextureAsyncLoader.create(fileName));
	}
	
	/**
	 * 释放资源
	 * @param fileName
	 */
	public void disposeAsset(String fileName){
		XXAsnycLoader asnycLoader = loaders.remove(fileName);
		if(asnycLoader != null){
			asnycLoader.dispose() ;
		}
	}
	
	/**
	 * 获取异步加载资源
	 * @param fileName
	 * @return
	 */
	public XXAsnycLoader getLoader(String fileName){
		return loaders.get(fileName);
	}
	
	public boolean isLoadFinish(){
		for(XXAsnycLoader loader : loaders.values()){
			if(!loader.isLoadFinish()){
				return false ;
			}
		}
		return true ;
	}
	
	@Override
	public void dispose() {
		for(XXAsnycLoader asnycLoader : loaders.values()){
			asnycLoader.dispose() ;
		}
		loaders.clear() ;
	}
}
